//
//  MyWelcomeViewController.swift
//  weibo08
//
//  Created by 陈杰明 on 16/3/2.
//  Copyright © 2016年 JM. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
//import SVProgressHUD

class MyWelcomeViewController: UIViewController {

    //记录头像约束的高度
//    var iconViewConstraint:NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        prepareUI()
        //先设置
        //设置用户头像
        setIcon()
        
        //获取用户头像
        MyUserAccountViewModel.sharedInstance.loadUserInfo { (error) -> () in
            if error != nil {
                print("error:\(error)")
                return
            }
            //设置用户头像
            self.setIcon()
        }
    }
    
    private func setIcon() {
        //设置用户头像
        if let avatar_large = MyUserAccountViewModel.sharedInstance.userAccount?.avatar_large {
            let url = NSURL(string: avatar_large)
            self.iconImageView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        moveAnimation()
    }
    //MARK: - 准备UI
    func prepareUI() {
        
    //添加子空间
        self.view.addSubview(bkgImageView)
        self.view.addSubview(iconImageView)
        self.view.addSubview(messageLabel)
        
        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
        iconImageView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        
    //添加约束 VFL
        //背景图片约束
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bkgView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkgView" : bkgImageView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[bkgView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkgView" : bkgImageView]))
        
//        bkgImageView.snp_makeConstraints { (make) -> Void in
//            make.edges.equalTo(UIEdgeInsetsZero)
//            
//            
//        }
        //头像约束
        
        view.addConstraint(NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute:NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
//        iconViewConstraint = NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute:NSLayoutAttribute.Bottom, multiplier: 1, constant:-160)
//        view.addConstraint(iconViewConstraint!)
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(view).offset(-160)
        }
        
        //约束宽高
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.width.height.equalTo(85)
        }
        
        //label约束
        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconImageView, attribute:NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconImageView, attribute:NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        
    }
    
    private func moveAnimation() {
    
//        iconViewConstraint?.constant = -(UIScreen.mainScreen().bounds.height - 160)
        iconImageView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view).offset(-(UIScreen.mainScreen().bounds.height - 160))
        }
        //实现弹簧效果
        //usingSpringWithDamping:弹簧明显程度
        UIView.animateWithDuration(1.25, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.messageLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        //跳转到MainViewController
                        AppDelegate.outSwitchRootViewCOntroller(true)
                })
        }
    
    }
    //MARK: - 懒加载
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconImageView: UIImageView = {
    
        let iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
        //设置圆角裁剪
        
            iconView.layer.cornerRadius = 42.5
        
            iconView.layer.masksToBounds = true
        
        return iconView
    }()
    
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        label.text = "欢迎归来"
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(14)
        
        //先隐藏
        label.alpha = 0
        return label
    }()
}
